﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Karo
{
    class Board
    {
        /// <summary>
        /// Maximum size of the game board
        /// </summary>
        public Size BoardSize { get; set; }
        public List<Pawn> firstPlayerPawns;
        public List<Pawn> secondPlayerPawns;
        public List<Tile> tileList;
        Player firstPlayer;
        Player secondPlayer;

        public Board(Player one, Player two)
        {
            this.firstPlayerPawns = new List<Pawn>();
            this.secondPlayerPawns = new List<Pawn>();
            this.tileList = new List<Tile>();
            this.firstPlayer = one;
            this.secondPlayer = two;

            for (int i = 0; i < 20; i++)
            {
                Tile t = new Tile(new Point(i, i));

                this.tileList.Add(t);
            }

            //For testing we are randomly placing the pawns on the tiles
            Random r = new Random();

            for (int i = 0; i < 6; i++)
            {
                Pawn pawn = new Pawn(firstPlayer);
                firstPlayerPawns.Add(pawn);
                while (pawn.currentTile == null)
                {
                    Tile randomTile = tileList[r.Next(0, tileList.Count)];
                    if (randomTile.pawn == null)
                    {
                        randomTile.pawn = pawn;
                        pawn.currentTile = randomTile;
                    }
                }
            }

            for (int i = 0; i < 6; i++)
            {
                Pawn pawn = new Pawn(secondPlayer);
                secondPlayerPawns.Add(pawn);
                while (pawn.currentTile == null)
                {
                    Tile randomTile = tileList[r.Next(0, tileList.Count)];
                    if (randomTile.pawn == null)
                    {
                        randomTile.pawn = pawn;
                        pawn.currentTile = randomTile;
                    }
                }
            }
        }


        /// <summary>
        /// Calculates and returns X and Y board coordinates based on relative mouse location
        /// </summary>
        /// <param name="mouseLocation">Relative mouse location</param>
        /// <returns>Board coordinates in X an Y</returns>
        public Point GetClickedCoordinates(Point mouseLocation)
        {
            return new Point(mouseLocation.X / Drawing.TileSize.Width, mouseLocation.Y / Drawing.TileSize.Height);
        }

        /// <summary>
        /// Returns tile if on the given coordinate is a tile,
        /// </summary>
        /// <param name="coordinates">Board coordinates</param>
        /// <returns>Tile on coordinates or null</returns>
        public Tile GetTileOnCoordinates(Point coordinates)
        {
            foreach (Tile currentTile in tileList)
                if (currentTile.Location == coordinates)
                    return currentTile;
            return null;
        }
    }
}
